package com.sqoq.downloader3.base_ddg

import android.app.Activity
import android.content.Context
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.annotation.LayoutRes
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.recyclerview.widget.RecyclerView

abstract class BaseRecyclerViewAdapter_ddg<T>(val context: Context) :
    RecyclerView.Adapter<BaseRecyclerViewAdapter_ddg.ViewHolder>() {
    val layoutInflater: LayoutInflater
    val data: MutableList<T> = ArrayList()
    var onItemClickListener: ((T, Int) -> Unit)? = null

    init {
        layoutInflater = LayoutInflater.from(context)

    }

    constructor(activity: Activity, data: List<T>) : this(activity) {
        setData(data)
    }

    open fun setData(data: List<T>?) {
        this.data.clear()
        if (data != null) {
            this.data.addAll(data)
        }
        notifyDataSetChanged()
    }

    fun clearAll() {
        data.clear()
        notifyDataSetChanged()
    }

    fun addData(data: T?) {
        if (data == null) {
            return
        }
        this.data.add(data)
        notifyDataSetChanged()
    }

    fun replace(position: Int, data: T?) {
        if (data == null) {
            return
        }
        this.data[position] = data
        notifyDataSetChanged()
    }

    fun addData(data: List<T>?) {
        if (data == null) {
            return
        }
        this.data.addAll(data)
        notifyDataSetChanged()
    }

    fun addToFirst(data: T?) {
        if (data == null) {
            return
        }
        this.data.add(0, data)
        notifyDataSetChanged()
    }

    fun removeByPosition(position: Int) {
        val count = data.size
        if (position < count) {
            data.removeAt(position)
            notifyDataSetChanged()
        }
    }

    fun remove(data: T?) {
        if (data == null) {
            return
        }
        this.data.remove(data)
        notifyDataSetChanged()
    }

    fun <T : ViewDataBinding> dataBindUi(@LayoutRes resource: Int): T {
        return DataBindingUtil.bind(layoutInflater.inflate(resource, null))!!
    }

    fun <T : ViewDataBinding> dataBindUi(
        @LayoutRes resource: Int,
        root: ViewGroup?,
        attachToRoot: Boolean
    ): T {
        return DataBindingUtil.bind(layoutInflater.inflate(resource, root, attachToRoot))!!
    }


    override fun getItemCount(): Int {
        return data.size
    }

    fun getItem(position: Int): T {
        return data[position]
    }


    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {

        return ViewHolder(
            DataBindingUtil.bind<ViewDataBinding>(
                layoutInflater.inflate(getItemLayout(viewType), parent, false)
            )!!
        )
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        if (holder.itemView != null && onItemClickListener != null) {
            holder.itemView.setOnClickListener {
                onItemClickListener?.invoke(
                    getItem(
                        position
                    ), position
                )
            }
        }
        bindData((holder as ViewHolder).mBinding, getItem(position), position)
    }

    //初始化view
    @LayoutRes
    protected abstract fun getItemLayout(viewType: Int): Int
    protected abstract fun bindData(dataBinding: ViewDataBinding, data: T, position: Int)
    class ViewHolder(var mBinding: ViewDataBinding) : RecyclerView.ViewHolder(
        mBinding.root
    )


}